<?php
namespace plugins\order\controller;
use cmf\controller\ApiPluginBaseController;
use think\Db;
use plugins\order\model\OrderMemberModel;
use plugins\order\model\OrderModel;
use plugins\store\model\StoreModel;
use plugins\order\model\OrderGoodsListModel;
use plugins\order\model\OrderConsigneeModel;
use plugins\goods\controller\ApiFreightPluginController;
use plugins\goods\model\GoodsModel;
use plugins\logistics\model\LogisticsModel;
//此处为订单数据获得方法接口
class ApiSelectOrderController extends ApiPluginBaseController
{
	//用户订单列表
	public function order_list()
	{
		//订单过期检查
		// ( new OrderMemberModel )->overTimeOrderCheck();
		$post = $this->request->post();
		$user = $this->getIdentity();
		if(empty($post)){
			return api_json_echo(false,'请传具体参数');
		}
		if(empty($post['page'])){
			$post['page'] = 1;
		}
		if(empty($post['page_size'])){
			$post['page_size'] = 5;
		}
		if(!empty($post['store']) && $post['store'] == true){
			$store = ( new StoreModel )->get(['uid'=>$user->uid]);
			if($store == null){
				return api_json_echo(false,'您的账户下并没有店铺');
			}
			$where['store_id'] = ['eq',$store->store_id];
		}else{
			$where = ['member_id'=>$user->uid];
		}
		// $where['order_status'] = ['neq','6'];
		$count = ( new OrderModel )->where($where)->count();
		if($count == 0){
			return api_json_echo(true,'该用户下无订单',['page'=>$post['page'],'page_size'=>$post['page_size'],'order'=>[]]);
		}
		if(!empty($post['order_status']) && $post['order_status'] != 0){
			$where['order_status'] = $post['order_status'];
			if($post['order_status'] == '4'){
				$where['order_status'] = ['in',['4','10']];
			}
		}
		$list = ( new OrderModel )->field('member_id',true)->order('submit_time desc')->page($post['page'],$post['page_size'])->where($where)->select()->toArray();
		foreach ($list as $key => &$value) {
			$store = ( new StoreModel )->get(['store_id'=>$value['store_id']]);
			if($store == null){
				$value['store_name'] = '该店铺不存在';
			}else{
				$value['store_name'] = $store->store_name;
			}
			//商品
			$goods_list = ( new OrderGoodsListModel )->where(['order_num'=>$value['order_num']])->select()->toArray();
			foreach ($goods_list as $k => &$v) {
                $goods = db('goods')->where(['id'=>$v['order_goods_id']])->find();
				$v['order_goods_img'] = zy_domain().$goods['goods_img'];
			}
			$value['goods_list'] = $goods_list;
			//邮费
			$consignee = (new OrderConsigneeModel)->get(['order_num'=>$value['order_num']]);
			if($consignee != null){
				$value['consignee_money'] = $consignee->order_consignee_money;
			}else{
				$value['consignee_money'] = 0;
			}

		}
		$data['page_size'] = $post['page_size'];
		$data['page'] = $post['page'];
		//总页数
		$data['count_page'] = ceil($count/$post['page_size']);
		$data['order'] = $list;
		return api_json_echo(true,'搜索完成',$data);
	}
	//用户订单详情
	public function order_detail()
	{
		$post = $this->request->post();
		checkParam(['order_num'=>['name'=>'订单编号']],$post);
		$user = $this->getIdentity();
		if(empty($post)){
			return api_json_echo(false,'请传具体参数');
		}
		$order = [];
		$order_model = ( new OrderModel )->field('member_id',true)->where(['order_num'=>$post['order_num']])->find();
		if($order_model == null){
			return api_json_echo(false,'没有指定订单');
		}
		$store = ( new StoreModel )->get(['store_id'=>$order_model->store_id]);
		if($store == null){
			$order['store_name'] = '该店铺不存在';
		}else{
			$order['store_name'] = $store->store_name;
		}
		// >=2 为已支付	支付时间
		if($order_model->order_status >= 2 ){
			if($order_model->paytime != null){
				$order_model->paytime = date('Y-m-d H:i:s',$order_model->paytime);
			}
		}
		// >=3 为已发货	发货时间
		if($order_model->order_status >= 3 ){
			$fhtime = (new LogisticsModel)->deliveryTime($order_model->order_num);
			if(!empty($fhtime)){
                $order_model->fhtime = date('Y-m-d H:i:s',$fhtime);
            }else{
                $order_model->fhtime = '';
            }
		}else{
			$order_model->fhtime = '';
		}
		switch ($order_model->money_type) {
			case NULL:
				$order_model->money_type = '未支付';
				break;
			case 'alipay':
				$order_model->money_type = '支付宝支付';
				break;
			case 'wechatpay':
				$order_model->money_type = '微信支付';
				break;
			case 'balance':
				$order_model->money_type = '余额支付';
				break;
			default:
				# code...
				break;
		}
		//订单主信息
		$order_model->submit_time = date('Y-m-d H:i:s',$order_model->submit_time);
		// $order_model->order_num = trim($order_model->order_num,'order_');
		$order['order'] = $order_model->toArray();
		//商品列表
		$goods_list = ( new OrderGoodsListModel )->where(['order_num'=>$order_model->order_num])->select()->toArray();
		foreach ($goods_list as $k => &$v) {
            $goods = db('goods')->where(['id'=>$v['order_goods_id']])->find();
            $v['order_goods_img'] = zy_domain().$goods['goods_img'];
		}
		$order['goods_list'] = $goods_list;
		//收货地址
		$consignee = ( new OrderConsigneeModel )->where(['order_num'=>$order_model->order_num])->find();
		$tmp = '';
		foreach (json_decode($consignee['order_consignee_address']) as $key => $value) {
			$tmp .= $value;
		}
		$consignee['order_consignee_address'] = $tmp;
		$order['consignee'] = $consignee;
		return api_json_echo(true,'查询完成',$order);
	}
	//用户订单统计
	public function orderStatistics()
	{
		$post = $this->request->post();
		$user = $this->getIdentity();
		$data = [];
		//待付款
		$data['obligation'] = ( new OrderModel )->where(['order_status'=>1,'member_id'=>$user->uid])->count();
		//待发货
		$data['pending'] = ( new OrderModel )->where(['order_status'=>2,'member_id'=>$user->uid])->count();
		//待收货
		$data['shipped'] = ( new OrderModel )->where(['order_status'=>3,'member_id'=>$user->uid])->count();
		//待评价
		$data['remain'] = ( new OrderModel )->where(['order_status'=>4,'member_id'=>$user->uid])->count();
		//退款/售后
		$data['aftersale'] =  ( new OrderModel )->where(['order_refund'=>['in',['2','3']],'member_id'=>$user->uid])->count();
		return api_json_echo(true,'',$data);
	}
	//用sku 计算运费
	public function countFreight()
	{
		$post = $this->request->post();
		//运费接口
		$freight = new ApiFreightPluginController();
		checkParam(['provice'=>['name'=>'省份'],'goods_id'=>['name'=>'商品id'],'goods_num'=>['name'=>'商品数量']],$post);
		$provice = $post['provice'];
		//运费
		$freight_money = 0;
		$goods_list = explode(',', $post['goods_id']);
		$num_list = explode(",", $post['goods_num']);
		foreach ($goods_list as $key => $value) {
			//商品信息
			$good = ( new GoodsModel )->get(['id'=>$value]);
			$data = $freight->getFreight($good->freight_id,$provice,$num_list[$key],$good->store_id,true);
			if($data['status'] != 'error'){
				$freight_money += $data['data']['freight_price'];
			}
			
		}
		return api_json_echo(true,'计算完成',$freight_money);
	}
}
